<template>
  <div class="flex flex-col h-full">
    <SearchContainer show-export @reset="() => formRef.resetFields()" @search="onSearch()" @export="exportRef.$el.querySelector('button').click()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="dateRange" label="收费日期">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="zjm" label="姓名/住院号" class="ml-4">
          <a-input v-model:value="formState.zjm" placeholder="请输入" allow-clear />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="flex-1">
      <a-space ref="exportRef" class="hidden">
        <BaseExport
          type="报表"
          name="住院病人费用明细查询"
          code="001056"
          is-show-time
          :search-params="{ ...formState, startDate: formState.dateRange?.[0], endDate: formState.dateRange?.[1] }"
          >导出</BaseExport
        >
      </a-space>
      <BaseTable row-key="id" column-code="001056" :loading="loading" :data-source="tableData" bordered fill :scroll="{ x: 2800 }" :pagination="pagination">
        <template #bodyCell="{ column, index }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
        </template>
        <template #summary>
          <a-table-summary fixed>
            <a-table-summary-row>
              <template v-for="column in columnsData" :key="column.dataIndex">
                <a-table-summary-cell v-if="column.dataIndex === 'xh'"> 合计 </a-table-summary-cell>
                <a-table-summary-cell v-else-if="!column.isHide">
                  <div :class="amountTatol[column.dataIndex] ? 'text-right' : 'text-center'">{{ amountTatol[column.dataIndex] || '-' }}</div>
                </a-table-summary-cell>
              </template>
            </a-table-summary-row>
          </a-table-summary>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'

const exportRef = ref(null)
const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().subtract(30, 'day').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})
const onSearch = () => {
  getData()
  getAmount()
}
const { loading, tableData, getData, pagination } = useTable({
  url: '/statistic/getInpBreakdownExpenses',
  params: formState.value,
  immediate: true,
  formatParams: (params) => {
    return {
      ...params,
      startDate: params.dateRange?.[0],
      endDate: params.dateRange?.[1]
    }
  }
})
//获取表头
const columnsData = ref([])
const getTableColumns = async () => {
  const { code, data } = await post('/common/util/getTableMappingCloumnList', { id: '001056' })
  if (code !== 0) return message.error(code)
  columnsData.value = data
}

const amountTatol = ref({})
const getAmount = async () => {
  const res = await post('/statistic/getInpBreakdownExpensesHj', {
    startDate: formState.value.dateRange?.[0],
    endDate: formState.value.dateRange?.[1],
    zjm: formState.value.zjm
  })
  if (res.code === 0) {
    amountTatol.value = res.data || {}
  }
}
onMounted(() => {
  getTableColumns()
  getAmount()
})
</script>
